//
//  AddViewController.m
//  扫码
//
//  Created by 鲍志刚 on 16/3/31.
//  Copyright © 2016年 鲍志刚. All rights reserved.
//

#import "ZGAddViewController.h"
#import "NetworkTools.h"
#import "ZGView.h"
#import "ZGColumnView.h"
#import "ZGBarChartViewController.h"
#import "ZGPieChartViewController.h"
#import "ZGLineChartViewController.h"
#import "ZGWaveChartViewController.h"


@interface ZGAddViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *startTime;

@property (weak, nonatomic) IBOutlet UITextField *endTime;
@property (weak, nonatomic) IBOutlet UITextField *stateTime;
@property (weak, nonatomic) IBOutlet UITextField *numberText;
@property (weak, nonatomic) IBOutlet UITextField *cityText;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) ZGView *cakeView;//饼状图

@property (nonatomic, strong) ZGColumnView *columnView;//柱状图

@property (nonatomic, strong) ZGView *lineView;//折线图

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) UIDatePicker *datePicker1;
@property (nonatomic, strong) UIDatePicker *datePicker2;

@property (nonatomic, strong) UIToolbar *toolbar1;
@property (nonatomic, strong) UIToolbar *toolbar2;


@end

@implementation ZGAddViewController

#pragma mark - 懒加载日期选择控件作为文本框的自定义键盘
- (UIDatePicker *)datePicker1 {
    if (!_datePicker1) {
        _datePicker1 = [[UIDatePicker alloc] init];
        
        // 设置时间格式
        _datePicker1.datePickerMode = UIDatePickerModeDateAndTime;
        
        // 本地化操作
        _datePicker1.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
        
    }
    return _datePicker1;
}


- (UIDatePicker *)datePicker2 {
    if (!_datePicker2) {
        _datePicker2 = [[UIDatePicker alloc] init];
        
        // 设置时间格式
        _datePicker2.datePickerMode = UIDatePickerModeDateAndTime;
        
        // 本地化操作
        _datePicker2.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
        
    }
    return _datePicker2;
}


#pragma mark - 懒加载toolbar
- (UIToolbar *)toolbar1 {
    if (!_toolbar1) {
        _toolbar1 = [[UIToolbar alloc] init];
        
        // 只需要设置高度就可以了
        _toolbar1.frame = CGRectMake(0, 0, 0, 44);
        
        // 设置toolbar的背景色
        _toolbar1.barTintColor = [UIColor grayColor];
        
        // 添加其他item
        // 弹簧
        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 完成
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done1)];
        
        // 添加到工具条内
        _toolbar1.items = @[flexItem, doneItem];
    }
    return _toolbar1;
}

- (UIToolbar *)toolbar2 {
    if (!_toolbar2) {
        _toolbar2 = [[UIToolbar alloc] init];
        
        // 只需要设置高度就可以了
        _toolbar2.frame = CGRectMake(0, 0, 0, 44);
        
        // 设置toolbar的背景色
        _toolbar2.barTintColor = [UIColor grayColor];
        
        // 添加其他item
        // 弹簧
        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 完成
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done2)];
        
        // 添加到工具条内
        _toolbar2.items = @[flexItem, doneItem];
    }
    return _toolbar2;
}



#pragma mark - 点击done的时候做一些事情
// 将时间数据显示在文本框上
- (void)done1 {
    
    // 1.获取日期数据
    NSDate *date = self.datePicker1.date;
    
    // 2.转为字符串数据
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *str = [formatter stringFromDate:date];
    
    // 3.设置时间字符串
    self.startTime.text = str;

    // 4.关闭键盘
    [self.view endEditing:YES];
    
}

- (void)done2 {
    
    // 1.获取日期数据
    NSDate *date = self.datePicker2.date;
    
    // 2.转为字符串数据
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *str = [formatter stringFromDate:date];
    
    // 3.设置时间字符串
    self.endTime.text = str;
    
    // 4.关闭键盘
    [self.view endEditing:YES];
    
}





- (NSArray *)dataArray {
    
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    
    return _dataArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = @[@"柱状图",
                       @"饼图",
                       @"折线图",
                       @"波浪图",
                       @"混合图",
                       @"图数据"
                       ];
    
    //设置tableView
    [self setUpUI];
    
    
    //自定义后退按钮
    [self addBackButton];
    
}

-(void)setUpUI{
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.startTime.inputView = self.datePicker1;
    self.startTime.inputAccessoryView = self.toolbar1;
    
    self.endTime.inputView = self.datePicker2;
    self.endTime.inputAccessoryView = self.toolbar2;
    
    
    
    
}

#pragma mark - 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    
    return self.dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    
    return cell;
    
}


#pragma mark - tableView代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        ZGBarChartViewController * vc = [[ZGBarChartViewController alloc] init];
    
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
        

    } else if (indexPath.row == 1) {
        
        
        ZGPieChartViewController *vc = [[ZGPieChartViewController alloc] init];
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 2) {
        
        ZGLineChartViewController *vc = [[ZGLineChartViewController alloc] init];

        
        [self.navigationController pushViewController:vc animated:YES];

    } else if (indexPath.row == 3) {
        
        ZGWaveChartViewController *vc = [[ZGWaveChartViewController alloc] init];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}


#pragma mark - 自定义后退按钮
- (void)addBackButton {
    
    //开启手势后退
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
//    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    //间距
    UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixed.width = 10;
    
    self.navigationItem.leftBarButtonItems = @[fixed,back];
}

#pragma mark - 返回

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
    
    
}



#pragma mark - 点击编辑以外的地方，退出键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    for (UITextField *text  in self.view.subviews) {
        
        if (![text isExclusiveTouch]) {
            [text resignFirstResponder];
        }
       
    }
    
    
    
}


#pragma mark - 时间查询

- (IBAction)searchBtn:(id)sender {
    
    NSLog(@"时间查询");
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
//    formatter.locale = 
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *startTime = [formatter dateFromString:self.startTime.text];
    
    NSDate *endTime = [formatter dateFromString:self.endTime.text];
    
    NSLog(@"%@,%@",startTime,endTime);

    //@"startTime":@'2016-1-1 12:12:12',@"endTime":@'2016-12-12 12:12:12'

        [[NetworkTools sharedTools] POST:@"SweepServlet?method=query" parameters:@{@"pageNum":@1,@"startTime":self.startTime.text,@"endTime":self.endTime.text,@"state":self.stateTime.text} progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
 
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@",error);
            
        }];
   

}

#pragma mark - 状态码
- (IBAction)stateBtn:(id)sender {
    
    NSLog(@"状态码");

    [[NetworkTools sharedTools] POST:@"SweepServlet?method=query" parameters:@{@"pageNum":[NSNumber numberWithInt:1],@"state":[NSNumber numberWithInt:[self.stateTime.text intValue]]} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - number

- (IBAction)numberBtn:(id)sender {
    
    NSLog(@"number");

    [[NetworkTools sharedTools] POST:@"SweepServlet?method=query" parameters:@{@"pageNum":[NSNumber numberWithInt:1],@"number":[NSNumber numberWithInt:[self.numberText.text intValue]]} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}



#pragma mark - 查询
- (IBAction)cityBtn:(id)sender {
    
    [[NetworkTools sharedTools] POST:@"SweepServlet?method=query" parameters:@{@"pageNum":[NSNumber numberWithInt:1],@"city":self.cityText.text,@"state":self.stateTime.text} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSDictionary *dic = responseObject;
        
        NSLog(@"%@",dic);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
}


#pragma mark - 饼状图
- (IBAction)cakeView:(UIButton *)sender {
    
    ZGView *cakeView = [[ZGView alloc] initWithFrame:CGRectMake(10, 340, 250, 250)];
    
    self.cakeView = cakeView;
    
    [self.view addSubview:self.cakeView];
    
    cakeView.backgroundColor = [UIColor whiteColor];
    
    sender.enabled = NO;

    NSLog(@"饼状图") ;
}




#pragma mark - 柱状图
- (IBAction)columnView:(id)sender {

    [self.cakeView removeFromSuperview];
    
    NSLog(@"柱状图");
}

#pragma mark - 折线图
- (IBAction)lineView:(id)sender {
    NSLog(@"折线图");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 懒加载
-(ZGView *)cakeView {
    
    if (_cakeView == nil) {
        
        _cakeView = [[ZGView alloc] initWithFrame:CGRectMake(10, 340, 250, 250)];
    }
    
    return _cakeView;
    
}


@end
